package com.ziyun.erp.modules.bonusRule.service.impl;

import io.renren.common.exception.RRException;
import io.renren.common.utils.R;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.erp.modules.bonusRule.dao.SellerBonusRuleDao;
import com.ziyun.erp.modules.bonusRule.entity.SellerBonusRuleEntity;
import com.ziyun.erp.modules.bonusRule.service.SellerBonusRuleService;


@Service("sellerBonusRuleService")
public class SellerBonusRuleServiceImpl extends ServiceImpl<SellerBonusRuleDao, SellerBonusRuleEntity> implements SellerBonusRuleService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<SellerBonusRuleEntity> page = this.selectPage(
                new Query<SellerBonusRuleEntity>(params).getPage(),
                new EntityWrapper<SellerBonusRuleEntity>()
                        .eq("del_flag",0)
                        .orderBy("start_time",false)
        );

        return new PageUtils(page);
    }

    @Override
    public void saveBonusRule(SellerBonusRuleEntity sellerBonusRule) {
        /*
        逻辑判断
        1.起时间 必须大于当前时间
        2.时间区间问题
        */
        Date currentDate = new Date();
        if (sellerBonusRule.getStartTime().compareTo(currentDate) == -1){
            throw new RRException("起时间 必须大于等于当前时间!");
        }

        // 按照起时间顺序排列 查询 比较
        List<SellerBonusRuleEntity> sellerBonusRuleList = this.selectList(new EntityWrapper<SellerBonusRuleEntity>()
                                                        .eq("del_flag",0)
                                                        .ge("end_time", currentDate)
                                                        .orderBy("start_time"));
        // 校验日期
        verificationTime(sellerBonusRule, sellerBonusRuleList);

        // 否则 则可以插入
        this.baseMapper.insert(sellerBonusRule);
    }

    @Override
    public void updateBonusRule(SellerBonusRuleEntity sellerBonusRule) {
           /*
        逻辑判断
        1.起时间 必须大于当前时间
        2.时间区间问题
        */
        Date currentDate = new Date();
        if (sellerBonusRule.getStartTime().compareTo(currentDate) == -1){
            throw new RRException("起时间 必须大于等于当前时间!");
        }

        // 按照起时间顺序排列 查询 更新 不加本条数据
        List<SellerBonusRuleEntity> sellerBonusRuleList = this.selectList(new EntityWrapper<SellerBonusRuleEntity>()
                .eq("del_flag",0)
                .ne("id", sellerBonusRule.getId())
                .ge("end_time", currentDate)
                .orderBy("start_time"));
        // 校验日期
        verificationTime(sellerBonusRule, sellerBonusRuleList);

        // 否则 则可以插入
        this.baseMapper.updateById(sellerBonusRule);

    }

    // 校验日期是否存在
    private void verificationTime(SellerBonusRuleEntity sellerBonusRule, List<SellerBonusRuleEntity> sellerBonusRuleList){
        for (SellerBonusRuleEntity temp : sellerBonusRuleList){
            if ((sellerBonusRule.getStartTime().after(temp.getStartTime()) && sellerBonusRule.getStartTime().before(temp.getEndTime()))
                    || sellerBonusRule.getStartTime().compareTo(temp.getStartTime()) == 0
                    ||  sellerBonusRule.getStartTime().compareTo(temp.getEndTime()) == 0){
                // 起时间 在 一条规则时间区间内 则数据 重叠
                throw new RRException("起时间 已经存在！");
            }

            if ((sellerBonusRule.getEndTime().after(temp.getStartTime()) && sellerBonusRule.getEndTime().before(temp.getEndTime()))
                    || sellerBonusRule.getEndTime().compareTo(temp.getStartTime()) == 0
                    || sellerBonusRule.getEndTime().compareTo(temp.getEndTime()) == 0){
                // 止时间 在 一条规则时间区间内 则数据 重叠
                throw new RRException("止时间 已经存在！");
            }
        }
    }

    /**
     * 根据指定日期 获取奖励金规则
     * @param time
     * @return
     */
    @Override
    public R getTimeBonusRule(Date time) {
        List<SellerBonusRuleEntity> sellerBonusRuleList =  this.selectList(new EntityWrapper<SellerBonusRuleEntity>()
                        .eq("del_flag", 0)
                        .le("start_time", time)
                        .ge("end_time", time));
        if (sellerBonusRuleList == null || sellerBonusRuleList.size() ==0){
            return R.error(100,"未查询到该时间内的 奖励金规则!");
        }

        if (sellerBonusRuleList.size() >1){
            return R.error(101,"奖励金规则配置异常,当前时间查出两条及以上 奖励金规则!");
        }

        return R.ok().put("multiple",sellerBonusRuleList.get(0).getMultiple());
    }


}
